/**
 * @param {Array} arr
 * @description 得到一个数组 遍历出各个关系
 */
export const generateData = (arr, n = 0) => {
  var list = []
  arr.forEach(val => {
    if (val.pid == n) {
      var data = {
        title: val.title,
        id: val.id,
        icon: val.icon,
      }
      var children = generateData(arr, val.id)
      if (children.length > 0) {
        data.children = children;
      }
      list.push(data)
    }
  })

  list = list.sort(function (a, b) { return a.sort - b.sort });

  return list
}



export const generateDataOptins = (arr, n = 0) => {
  var list = generateData(arr, n);
  var j = 1;

  function getOPtions(list, j = 0) {
    var arrrays = []
    // if (j > -1) {
    //   j++
    // } else {
    //   j = 0
    // }
    var lin = ''
    for (var i = 0; i < j; i++) {
      lin += '--'
    }
    list.forEach(val => {
      var data = {
        title: lin + val.title,
        id: val.id,
        icon: val.icon,
      }
      arrrays.push(data)
      if (val.children && val.children.length > 0) {
        var datas = getOPtions(val.children, j);
        arrrays = [...arrrays, ...datas]
      }
    })
    return arrrays
  }
  return getOPtions(list)

}